# coding=utf-8

import sys
from thrift.protocol.TBinaryProtocol import TBinaryProtocol  
from thrift.transport.TTransport import TMemoryBuffer
sys.path.append('..')
from bdp.i_crawler.i_entity_extractor.ttypes import EntityExtractorInfo
from i_util.normal_proccessor import NormalProccessor

import common
from data_saver import datasaver

# beanstalk的入口
class DataSaverProccessor(NormalProccessor):
    def __init__(self, log, thread_num):
        self.log = log
        self.data_saver = datasaver

    def do_task(self, body):
        # 从body中解析出EntityExtractorInfo
        entity_info = EntityExtractorInfo()
        try:
            tMemory_o = TMemoryBuffer(body)  
            tBinaryProtocol_o = TBinaryProtocol(tMemory_o)  
            entity_info.read(tBinaryProtocol_o)
        except EOFError, e:
            self.log.warning("cann't read EntityExtractorInfo from string");
            return None;
        self.log.debug('DataSaverProccessor: accept new entity !')

        # 开始干活
        self.data_saver.save_data(entity_info)
        return None

    def do_output(self,body):
        return True;
